Simplified the encoding logic.