Refactor codepage encoding functions