turn generator into function
It seems generator code has bugs?