Optimize and refactor decoder