fixes funciton to copy array before working on it