refactored decoder to use less functions