fixing a bug in bag implementation