Rebase of PR 25

See #25 for discussion. This is a rebase to current master.