refactored a bunch of code and added javadoc comments