Fixed bugs, added javadoc