Fixed issue #1. Probably needs some more thorough testing.