Added unit tests for canonicalization and fixed several bugs therein