#57 Fixed some bugs and added javadoc and also refactored.