added documentation. cleaned up some code.