diff --git a/pkg/local_object_storage/pilorama/boltdb.go b/pkg/local_object_storage/pilorama/boltdb.go
index 1487296a4..29a9306b6 100644
--- a/pkg/local_object_storage/pilorama/boltdb.go
+++ b/pkg/local_object_storage/pilorama/boltdb.go
@@ -1069,15 +1069,7 @@ func (t *boltForest) TreeSortedByFilename(ctx context.Context, cid cidSDK.ID, tr
 	}
 
 	if fewChildren {
-		sort.Slice(result, func(i, j int) bool {
-			return bytes.Compare(result[i].Meta.GetAttr(AttributeFilename), result[j].Meta.GetAttr(AttributeFilename)) == -1
-		})
-		for i := range result {
-			if bytes.Compare([]byte(last), result[i].Meta.GetAttr(AttributeFilename)) == -1 {
-				result = result[i:]
-				break
-			}
-		}
+		result = sortAndCut(result, []byte(last))
 	}
 	if len(result) != 0 {
 		last = string(result[len(result)-1].Meta.GetAttr(AttributeFilename))
@@ -1085,6 +1077,18 @@ func (t *boltForest) TreeSortedByFilename(ctx context.Context, cid cidSDK.ID, tr
 	return result, last, metaerr.Wrap(err)
 }
 
+func sortAndCut(result []NodeInfo, last []byte) []NodeInfo {
+	sort.Slice(result, func(i, j int) bool {
+		return bytes.Compare(result[i].Meta.GetAttr(AttributeFilename), result[j].Meta.GetAttr(AttributeFilename)) == -1
+	})
+	for i := range result {
+		if bytes.Compare(last, result[i].Meta.GetAttr(AttributeFilename)) == -1 {
+			return result[i:]
+		}
+	}
+	return nil
+}
+
 func (t *boltForest) getChildInfo(b *bbolt.Bucket, key []byte, childID Node) (NodeInfo, error) {
 	childInfo := NodeInfo{ID: childID}
 	parentID, _, metaBytes, found := t.getState(b, stateKey(key, childID))