Major refactoring. Added javadoc comments to most methods.