Refactor encoding context initialization to a func