fixed bug which resulted in the smallest index not necessarily being first