updated for Python3, should work in 2 and 3 now, but requires the six module