Fixed everything from my earlier pull request.

I have fixed everything from my earlier pull request. Hopefully it's mergeable now.