Huge refactoring and PEP8 fixes. WARNING: not compatible with old version, but easy to migrate