diff --git a/api/graphql/models/utils.go b/api/graphql/models/utils.go
index 6a446b4..b28aa74 100644
--- a/api/graphql/models/utils.go
+++ b/api/graphql/models/utils.go
@@ -1,50 +1,36 @@
 package models
 
 import (
-	"fmt"
-	"log"
+	"gorm.io/gorm"
+	"gorm.io/gorm/clause"
 )
 
-func (filter *Filter) FormatSQL(context string) (string, error) {
-	// TODO: Migrate method to gorm
-
-	if filter == nil {
-		return "", nil
-	}
-
-	orderByMap := make(map[string]string)
-	orderByMap["media_date_shot"] = "media.date_shot"
-	orderByMap["media_date_imported"] = "media.date_imported"
-	orderByMap["media_title"] = "media.title"
-	orderByMap["media_kind"] = "media.media_type, SUBSTRING_INDEX(media.path, '.', -1)"
-	orderByMap["album_title"] = "album.title"
-
-	result := ""
-
-	if filter.OrderBy != nil {
-		order_by, ok := orderByMap[context+"_"+*filter.OrderBy]
-		if !ok {
-			log.Printf("Invalid order column: '%s'\n", *filter.OrderBy)
-			return "", nil
-		}
-
-		direction := "ASC"
-		if filter.OrderDirection != nil && filter.OrderDirection.IsValid() {
-			direction = filter.OrderDirection.String()
-		}
-
-		result += fmt.Sprintf(" ORDER BY %s %s", order_by, direction)
-	}
+func (filter *Filter) FormatSQL(tx *gorm.DB) *gorm.DB {
 
 	if filter.Limit != nil {
-		offset := 0
-		if filter.Offset != nil && *filter.Offset >= 0 {
-			offset = *filter.Offset
-		}
-
-		result += fmt.Sprintf(" LIMIT %d OFFSET %d", *filter.Limit, offset)
+		tx.Limit(*filter.Limit)
 	}
 
-	log.Printf("SQL Filter: '%s'\n", result)
-	return result, nil
+	if filter.Offset != nil {
+		tx.Offset(*filter.Offset)
+	}
+
+	if filter.OrderBy != nil {
+
+		desc := true
+		if filter.OrderDirection != nil && filter.OrderDirection.IsValid() {
+			if *filter.OrderDirection == OrderDirectionAsc {
+				desc = false
+			}
+		}
+
+		tx.Order(clause.OrderByColumn{
+			Column: clause.Column{
+				Name: *filter.OrderBy,
+			},
+			Desc: desc,
+		})
+	}
+
+	return tx
 }
diff --git a/api/graphql/resolvers/album.go b/api/graphql/resolvers/album.go
index 92701e9..fa97759 100644
--- a/api/graphql/resolvers/album.go
+++ b/api/graphql/resolvers/album.go
@@ -30,7 +30,7 @@ func (r *queryResolver) MyAlbums(ctx context.Context, filter *models.Filter, onl
 		query = query.Where("EXISTS (?)", subQuery)
 	}
 
-	// TODO: Incorporate models.FormatSQL
+	query = filter.FormatSQL(query)
 
 	var albums []*models.Album
 	if err := query.Find(&albums).Error; err != nil {
@@ -71,7 +71,7 @@ func (r *albumResolver) Media(ctx context.Context, album *models.Album, filter *
 		query = query.Where("media.favorite = 1")
 	}
 
-	// TODO: Incorporate filter.FormatSQL
+	query = filter.FormatSQL(query)
 
 	var media []*models.Media
 	if err := query.Find(&media).Error; err != nil {
@@ -109,12 +109,14 @@ func (r *albumResolver) Thumbnail(ctx context.Context, obj *models.Album) (*mode
 func (r *albumResolver) SubAlbums(ctx context.Context, parent *models.Album, filter *models.Filter) ([]*models.Album, error) {
 
 	var albums []*models.Album
-	if err := r.Database.Where("parent_album_id = ?", parent.ID).Find(&albums).Error; err != nil {
+
+	query := r.Database.Where("parent_album_id = ?", parent.ID)
+	query = filter.FormatSQL(query)
+
+	if err := query.Find(&albums).Error; err != nil {
 		return nil, err
 	}
 
-	// TODO: Incorporate filter.FormatSQL
-
 	return albums, nil
 }
 
diff --git a/api/graphql/resolvers/media.go b/api/graphql/resolvers/media.go
index 5ec5c6e..c50a9b7 100644
--- a/api/graphql/resolvers/media.go
+++ b/api/graphql/resolvers/media.go
@@ -17,22 +17,16 @@ func (r *queryResolver) MyMedia(ctx context.Context, filter *models.Filter) ([]*
 		return nil, errors.New("unauthorized")
 	}
 
-	// filterSQL, err := filter.FormatSQL("media")
-	// if err != nil {
-	// 	return nil, err
-	// }
-
 	var media []*models.Media
 
-	// TODO: Incorporate filter.FormatSQL
-
-	err := r.Database.
+	query := r.Database.
 		Joins("Album").
 		Where("albums.owner_id = ?", user.ID).
-		Where("media.id IN (?)", r.Database.Model(&models.MediaURL{}).Select("id").Where("media_url.media_id = media.id")).
-		Scan(&media).Error
+		Where("media.id IN (?)", r.Database.Model(&models.MediaURL{}).Select("id").Where("media_url.media_id = media.id"))
 
-	if err != nil {
+	query = filter.FormatSQL(query)
+
+	if err := query.Scan(&media).Error; err != nil {
 		return nil, err
 	}
 
@@ -77,18 +71,8 @@ func (r *queryResolver) MediaList(ctx context.Context, ids []int) ([]*models.Med
 		Joins("Album").
 		Where("media.id IN ?", ids).
 		Where("album.owner_id = ?", user.ID).
-		// Where("media.id IN (?)", r.Database.Model(&models.MediaURL{}).Select("media_id").Where("media_url.media_id = media.id")).
 		Scan(&media).Error
 
-	// err := r.Database.Raw(`
-	// 	SELECT media.* FROM media
-	// 	JOIN albums AS album ON media.album_id = album.id
-	// 	WHERE media.media_id IN ? AND album.owner_id = ?
-	// 	AND media.media_id IN (
-	// 		SELECT media_id FROM media_url WHERE media_url.media_id = media.media_id
-	// 	)
-	// `, ids, user.ID).Error
-
 	if err != nil {
 		return nil, errors.Wrap(err, "could not get media list by media_id and user_id from database")
 	}
diff --git a/api/graphql/resolvers/user.go b/api/graphql/resolvers/user.go
index 52576b6..663d673 100644
--- a/api/graphql/resolvers/user.go
+++ b/api/graphql/resolvers/user.go
@@ -10,22 +10,11 @@ import (
 	"gorm.io/gorm"
 )
 
-// func (r *Resolver) User() UserResolver {
-// 	return &userResolver{r}
-// }
-
-// type userResolver struct{ *Resolver }
-
 func (r *queryResolver) User(ctx context.Context, filter *models.Filter) ([]*models.User, error) {
 
-	filterSQL, err := filter.FormatSQL("user")
-	if err != nil {
-		return nil, err
-	}
-
 	var users []*models.User
-	err = r.Database.Raw("SELECT * FROM users" + filterSQL).Scan(&users).Error
-	if err != nil {
+
+	if err := filter.FormatSQL(r.Database.Model(models.User{})).Error; err != nil {
 		return nil, err
 	}
 
diff --git a/api/scanner/cleanup_media.go b/api/scanner/cleanup_media.go
index d47791c..70053ab 100644
--- a/api/scanner/cleanup_media.go
+++ b/api/scanner/cleanup_media.go
@@ -81,7 +81,7 @@ func deleteOldUserAlbums(db *gorm.DB, scannedAlbums []*models.Album, user *model
 	albumIDs := make([]int, 0)
 	for _, album := range albums {
 		albumIDs = append(albumIDs, album.ID)
-		cachePath := path.Join("./photo_cache", strconv.Itoa(int(album.ID)))
+		cachePath := path.Join(PhotoCache(), strconv.Itoa(int(album.ID)))
 		err := os.RemoveAll(cachePath)
 		if err != nil {
 			deleteErrors = append(deleteErrors, errors.Wrapf(err, "delete unused cache folder (%s)", cachePath))
