Rewritten for use as Windows service.