Handling zero results

These changes handle zero results (fixing the index out of range exception in case of zero results).