Clean up code and add some comments