Fixed empty array bug

This fixes bug #10. Also added a test.

Tidied up code using pyflakes and pep8.