Typos and tweaks

Hi @skendrew

Just a heads up that I've started playing with your code. I found and fixed a few typos that prevented the example script from running on my machine. In addition, I found a few other performance hotspots, which I tweaked to be faster. I don't think they should affect the results.

Feel free to pull in these changes, or ignore them