Cleaned up code and added appropriate documentation.