Reimplement shuffle operation to use less memory