Cleaning up code and adding more comments