Added some javadoc and refactored somewhat