Fix on the inifinte loop bug. 'Tain't pretty, but it works.