Fix stack overflow when handling very, very large numbers of coordinates

This PR fixes #1891. See the first commit for explanation.