Adding Charlie's changes, which fixes #22 and fixes #20