Fixed out logic issue.
Issue with array length == 0 still exist.