refactoring of palette code