Clean up code and add Javadocs