Added check for non-existing neighbors