Initial commit

This is the code as I initally sent it to you. I'm going to make a few more pull requests with the different bugfixes I've done.