properly clean resources when running in server mode