Cleaned up code and added documentation.