refactor code to introduce generators