Refactorized and added javadoc comments.