add javadocs and clean up code