Bad code smell here, 2 `while` loops, 2 `for` loops, `continue`'s and `break`'s just smells bad to me.