Refactor some code and add basic support for UI scaling