Segfaults when it wants to exit the loop?