Cleaning up code and adding comments