Clean up code and add comments