Extract main loop (closes #74)