Main loop. Also fixed some syntax issues.