Make message queue functions more robust