fixed bug for empty arrays and added test.