Refactor code and add javadocs.