Fixed some bugs.

I tried using this yesterday and had some problems, so I fixed them.

I made three main changes, described at the beginning comments. The code was changed in five places, and each spot that was changed is preceded by a comment that starts with "John's Addition." I'm new to github, so I welcome any tips if I'm using the fork and pull request features wrong.