Refactored code and javadocs