Cleaned up and pythonified bunch of changes ;)

Hi,

I've made some changes to make bunch clean and simple, readable, and to follow python conventions.

It passes all the tests.
Hope you'll like it ;)

Best regards,
Paweł