Restructuring the code to make it easier to understand
This is an effort to make this code a little bit more understandable than it was before. Renaming variables, removing unnecessary code, cleaning up comments and adding functions are some of the work withing this MR.