Refactored, make it easier to extend to other encoding