Gabriel G: generalize type encode functions