Fixing array.splice() issue