Refactoring

With python 3 compatibility!