added check if server is still running before trying to call wait