Fixes issue #18

Fixes issue #18, I think. Please check through it and make sure it's sane.