Refactor some code so that it's easier to make a Cairo-less implementation