refactored shuffle function