Proposed fix for #1280

This PR proposes a fix for #1280, and #1676.

It is not ready to be merged, as I need a few more tests. 

Input for tests are very welcome.
I think we should cache this. Making multiple calls would means we end up splitting (and encoding) it over and over again.