Final refactoring and cleanup, added javadocs