implementation of detectGoalState