Added a function so that proper encoding can happen anywhere it needs to.