Added NULL check to dequeue_thread() and improved commenting