Rearranging the code and adding in line comments