It was working as long as the coordinates in the map were ordered, but since this is not ensured, we need a more complex way of checking for them.