implementing decode loop