Final code, after tweaking and testing. Also updated javadocs and
comments