Code cleaning, refactoring and adding javadocs