Make pythonic, remove bugs and crashes