Fixed sortedArray implementation