fix state key generation
add test coverage