Added code to avoid memory leak with empty queues. Added few tests