Added timeout to mg_stop function.