Refactored codec emitting to use context and using-based loops