cleaned up code and documented almost everything.