Refactor trySetValue and friends.