Refactored some code and added javadoc documents