Add javadocs and refactor