Fix bugs 22 and 23.

If you disagree with some of the changes, let me know, I'll add more (if it's possible) or split them into multiple pull requests.