Fix bugs in #22

Fixed bugs reported in #22.
See commits for details.