Refactor reverseArray method