refactor some methods and simplfied the generator