Add tests for queue_remove and fix memory leak