messy removal of dependance of allStates, but working
cleanup and optimize