updated with cleaned up code and javadoc