Code clean up, add preconditions, and correct Javadocs.