main.c: fixed logic that was causing a race condition