Improvd version using states