Cleaned up the code and documented