Merge @jarrettchisholm's change plus add some tests. Thanks!

Fixes #37 and #39.