Major refactoring to idiomatic python
Further refactoring