added more documentation, cleaned up some code