Fixed the endless loop bug
Fixed minor bugs
Added Javadoc