Clean up code and add more comments