Support for ST3, while keeping support for ST2.

It seems like the ST3 branch is only partially working, e.g. entity encode does not.

This is an alternative fix for ST3 which keeps all the previous functionality as well as support for ST2, while making it work exactly the same in ST3.

I will be commenting the pieces of code in this pull request to explain it a bit more.