refactored code, added Javadocs