This is (afaik) the only function left with GOTOs. I didn't manage to refactor it without rewriting everything... Help is welcome! :)