Fixed a number of small bugs (mutable args, use of undefined variables etc) and hopefully shut pep8 up.