refactor code to a extensible state