Back to original logic with debugging