Partial rewrite.

Learned more about python since the last update and fixed some bugs.