changed getClosestState to return state instead of index