split capture_loop from capture.c, some more code cleanup