refactor and add javadoc