Cleaned up the code and wrote the documentation.