Separated into a module, future proofed a little and made more pythonic