Really fix messed up control flow... still a bit gnarly, but it works