Some more refactoring. Stuck at the decode function.