state refactoring working. Now make it generic