Clean up code, add javadoc