Clean up code and add documentation