Added javadoc comments and fixed various bugs.