Refactoring and added javadoc