Fix pyflakes and test errors