refactored and added javadoc