Clean up

Cleaned up and documented the code.