Refactor normalizeArray function