Clean up code and add more documentation