added check for empty array