Fixed memory leak and extracted encoding dict builder.