fixed bug where non-sorted arrays returned the incorrect image