encoding reimplementation