Added javadoc and cleaned up code