cleaned up code and added documentation