Cleaning up code and adding documentation.