Cleaned up some code and added some javadoc