Shuflle method and exception handling

Optimized the shuffling method and added exception handling