Fixed segfault in main loop