moved ksort of array to ensure the data is set first.