Cleaning up the code and adding comments in the new functions