Cleaned up code and add some javadocs