Cleaned up code and documented