Fix dp bug, and add sanity check to encoder.