working packing algo