fixing index error for top sort