Made much more robust when running in server mode