remove dead code, reorganize state logic