Refactoring mostly and generator finally works