Fix cmp code (issue #19) and add doctests.