Reworked control flow, better debugging output