diff --git a/CHANGELOG.md b/CHANGELOG.md
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,9 @@
+## 0.10.2.1
+
+* Fixed bug that generated broken queries when using ordered
+  `aggregateOrdered` with `distinctAggregator`, and when using set
+  aggregation.
+
 ## 0.10.2.0
 
 * Added `isJustAnd`
diff --git a/LICENSE b/LICENSE
--- a/LICENSE
+++ b/LICENSE
@@ -1,4 +1,4 @@
-Copyright (c) 2014-2018 Purely Agile Limited; 2019-2023 Tom Ellis
+Copyright (c) 2014-2018 Purely Agile Limited; 2019-2024 Tom Ellis
 
 All rights reserved.
 
diff --git a/Test/Opaleye/Test/Arbitrary.hs b/Test/Opaleye/Test/Arbitrary.hs
--- a/Test/Opaleye/Test/Arbitrary.hs
+++ b/Test/Opaleye/Test/Arbitrary.hs
@@ -471,7 +471,7 @@
 genSelectMapper :: [TQ.Gen (O.Select Fields -> O.Select Fields)]
 genSelectMapper =
     [ do
-        return (O.distinctExplicit distinctFields)
+        return (O.distinctExplicit unpackFields distinctFields)
     , do
         ArbitraryPositiveInt l <- TQ.arbitrary
         return (O.limit l)
@@ -481,9 +481,8 @@
     , do
         o                <- TQ.arbitrary
         return (O.orderBy (arbitraryOrder o))
-
     , do
-        return (O.aggregate aggregateFields)
+        return (O.aggregateExplicit unpackFields aggregateFields)
     , do
         let q' q = P.dimap (\_ -> fst . firstBoolOrTrue (O.sqlBool True))
                            (fieldsList
diff --git a/Test/QuickCheck.hs b/Test/QuickCheck.hs
--- a/Test/QuickCheck.hs
+++ b/Test/QuickCheck.hs
@@ -432,7 +432,7 @@
 
 distinct :: ArbitrarySelect -> Connection -> IO TQ.Property
 distinct =
-  compareDenotation' (O.distinctExplicit distinctFields) nub
+  compareDenotation' (O.distinctExplicit unpackFields distinctFields) nub
 
 -- When we generalise compareDenotation... we can just test
 --
@@ -455,7 +455,7 @@
 
 aggregate :: ArbitrarySelect -> Connection -> IO TQ.Property
 aggregate =
-  compareDenotationNoSort' (O.aggregate aggregateFields)
+  compareDenotationNoSort' (O.aggregateExplicit unpackFields aggregateFields)
                            aggregateDenotation
 
 
diff --git a/Test/Test.hs b/Test/Test.hs
--- a/Test/Test.hs
+++ b/Test/Test.hs
@@ -15,6 +15,7 @@
 import qualified Data.Function                    as F
 import           Data.Int (Int32)
 import qualified Data.List                        as L
+import qualified Data.List.NonEmpty               as NE
 import qualified Data.Ord                         as Ord
 import qualified Data.Profunctor                  as P
 import qualified Data.Profunctor.Product          as PP
@@ -34,7 +35,7 @@
 import qualified Opaleye.Field                    as F
 import qualified Opaleye.Internal.Aggregate       as IA
 import qualified Opaleye.Internal.Column          as O (unColumn)
-import qualified Opaleye.Internal.HaskellDB.PrimQuery as O (PrimExpr (..))
+import qualified Opaleye.Internal.HaskellDB.PrimQuery as O (AggrOp (..), PrimExpr (..))
 import qualified Opaleye.Internal.Operators       as O (relationValuedExpr)
 import           Opaleye.Internal.RunQuery        (DefaultFromField)
 import           Opaleye.Internal.MaybeFields     as OM
@@ -617,6 +618,36 @@
                      ]
         sortedData = L.sortBy (Ord.comparing snd) table7data
 
+
+testStringArrayAggregateOrderedDistinct :: Test
+testStringArrayAggregateOrderedDistinct = it "" $ q `selectShouldReturnSorted` expected
+  where q =
+          O.aggregateOrdered
+            (O.asc snd)
+            (PP.p2 (O.arrayAgg, O.distinctAggregator . O.stringAgg . O.sqlString $ ","))
+            table7Q
+        expected = [ ( map fst sortedData
+                     , L.intercalate "," $ map NE.head $ NE.group $ map snd sortedData
+                     )
+                   ]
+        sortedData = L.sortBy (Ord.comparing snd) table7data
+
+-- See
+--
+--     https://github.com/tomjaguarpaw/haskell-opaleye/pull/578#issuecomment-1782638274
+testStringArrayAggregateOrderedDistinctDuplicateFields :: Test
+testStringArrayAggregateOrderedDistinctDuplicateFields = xit "" $ q `selectShouldReturnSorted` expected
+  where q =
+          O.aggregateOrdered
+            (O.asc (\x -> snd x O..++ snd x))
+            (PP.p2 (O.arrayAgg, O.distinctAggregator . O.stringAgg . O.sqlString $ ","))
+            table7Q
+        expected = [ ( map fst sortedData
+                     , L.intercalate "," $ map NE.head $ NE.group $ map snd sortedData
+                     )
+                   ]
+        sortedData = L.sortBy (Ord.comparing snd) table7data
+
 -- | Using orderAggregate you can apply different orderings to
 -- different aggregates.
 
@@ -1444,7 +1475,26 @@
         expectation :: [(Int32, T.Text)]
         expectation = zipWith (,) as bs
 
+testSetAggregate :: Test
+testSetAggregate = do
+  it "set aggregate (percentile_cont)" $ testH query (`shouldBe` [expectation])
+  where query :: Select (Field O.SqlFloat8)
+        query = O.aggregate median (O.values as)
 
+        median :: IA.Aggregator (Field O.SqlFloat8) (Field O.SqlFloat8)
+        median = percentileCont 0.5
+
+        percentileCont :: O.Field O.SqlFloat8 -> IA.Aggregator (Field O.SqlFloat8) (Field O.SqlFloat8)
+        percentileCont fraction = IA.withinGroup (O.asc id) (P.lmap (const fraction) aggr)
+          where
+            aggr = IA.makeAggr (O.AggrOther "percentile_cont")
+
+        as :: [Field O.SqlFloat8]
+        as = [1, 2, 3, 4]
+
+        expectation :: Double
+        expectation = 2.5
+
 main :: IO ()
 main = do
   let envVarName = "POSTGRES_CONNSTRING"
@@ -1523,8 +1573,11 @@
         testOverwriteAggregateOrdered
         testMultipleAggregateOrdered
         testStringArrayAggregateOrdered
+        testStringArrayAggregateOrderedDistinct
+        testStringArrayAggregateOrderedDistinctDuplicateFields
         testDistinctAndAggregate
         testDoubleAggregate
+        testSetAggregate
       describe "distinct" $ do
         testDistinct
       describe "distinct on"
diff --git a/opaleye.cabal b/opaleye.cabal
--- a/opaleye.cabal
+++ b/opaleye.cabal
@@ -1,6 +1,6 @@
 name:            opaleye
-copyright:       Copyright (c) 2014-2018 Purely Agile Limited; 2019-2023 Tom Ellis
-version:         0.10.2.0
+copyright:       Copyright (c) 2014-2018 Purely Agile Limited; 2019-2024 Tom Ellis
+version:         0.10.2.1
 synopsis:        An SQL-generating DSL targeting PostgreSQL
 description:     An SQL-generating DSL targeting PostgreSQL.  Allows
                  Postgres queries to be written within Haskell in a
@@ -24,6 +24,9 @@
 
 library
   default-language: Haskell2010
+  default-extensions: MultiParamTypeClasses,
+                      FlexibleContexts,
+                      FlexibleInstances
   hs-source-dirs: src
   build-depends:
       aeson               >= 0.6     && < 2.3
diff --git a/src/Opaleye/Aggregate.hs b/src/Opaleye/Aggregate.hs
--- a/src/Opaleye/Aggregate.hs
+++ b/src/Opaleye/Aggregate.hs
@@ -1,4 +1,5 @@
 {-# LANGUAGE DataKinds #-}
+{-# LANGUAGE FlexibleContexts #-}
 
 -- | Perform aggregation on 'S.Select's.  To aggregate a 'S.Select' you
 -- should construct an 'Aggregator' encoding how you want the
@@ -33,10 +34,14 @@
        , stringAgg
        -- * Counting rows
        , countRows
+       -- * Explicit
+       , aggregateExplicit
        ) where
 
+import           Control.Arrow (second, (<<<))
 import           Data.Profunctor     (lmap)
 import qualified Data.Profunctor as P
+import qualified Data.Profunctor.Product.Default as D
 
 import qualified Opaleye.Internal.Aggregate as A
 import           Opaleye.Internal.Aggregate (Aggregator, orderAggregate)
@@ -45,6 +50,7 @@
 import qualified Opaleye.Internal.HaskellDB.PrimQuery as HPQ
 import qualified Opaleye.Internal.Operators as O
 import qualified Opaleye.Internal.PackMap as PM
+import           Opaleye.Internal.Rebind (rebindExplicit)
 import qualified Opaleye.Internal.Tag as Tag
 import qualified Opaleye.Internal.Unpackspec as U
 
@@ -84,11 +90,8 @@
 -}
 -- See 'Opaleye.Internal.Sql.aggregate' for details of how aggregating
 -- by an empty query with no group by is handled.
-aggregate :: Aggregator a b -> S.Select a -> S.Select b
-aggregate agg q = Q.productQueryArr $ do
-  (a, pq) <- Q.runSimpleSelect q
-  t <- Tag.fresh
-  pure (A.aggregateU agg (a, pq, t))
+aggregate :: D.Default U.Unpackspec a a => Aggregator a b -> S.Select a -> S.Select b
+aggregate = aggregateExplicit D.def
 
 -- | Order the values within each aggregation in `Aggregator` using
 -- the given ordering. This is only relevant for aggregations that
@@ -99,7 +102,13 @@
 -- you need different orderings for different aggregations, use
 -- 'Opaleye.Internal.Aggregate.orderAggregate'.
 
-aggregateOrdered  :: Ord.Order a -> Aggregator a b -> S.Select a -> S.Select b
+aggregateExplicit :: U.Unpackspec a a' -> Aggregator a' b -> S.Select a -> S.Select b
+aggregateExplicit u agg q = Q.productQueryArr $ do
+  (a, pq) <- Q.runSimpleSelect (rebindExplicit u <<< q)
+  t <- Tag.fresh
+  pure (second ($ pq) (A.aggregateU agg (a, t)))
+
+aggregateOrdered  :: D.Default U.Unpackspec a a => Ord.Order a -> Aggregator a b -> S.Select a -> S.Select b
 aggregateOrdered o agg = aggregate (orderAggregate o agg)
 
 -- | Aggregate only distinct values
diff --git a/src/Opaleye/Binary.hs b/src/Opaleye/Binary.hs
--- a/src/Opaleye/Binary.hs
+++ b/src/Opaleye/Binary.hs
@@ -32,8 +32,6 @@
 -- @Control.Applicative.Alternative@ instance but it fails to work
 -- only because of the typeclass constraint it has.
 
-{-# LANGUAGE MultiParamTypeClasses, FlexibleContexts #-}
-
 module Opaleye.Binary (-- * Binary operations
                        unionAll,
                        union,
diff --git a/src/Opaleye/Distinct.hs b/src/Opaleye/Distinct.hs
--- a/src/Opaleye/Distinct.hs
+++ b/src/Opaleye/Distinct.hs
@@ -1,5 +1,3 @@
-{-# LANGUAGE FlexibleContexts #-}
-
 module Opaleye.Distinct (distinct,
                          distinctOn,
                          distinctOnBy,
@@ -20,6 +18,7 @@
 import           Opaleye.Order
 
 import qualified Data.Profunctor.Product.Default as D
+import Opaleye.Internal.Unpackspec (Unpackspec)
 
 -- | Remove duplicate rows from the 'Select'.
 --
@@ -42,5 +41,6 @@
 -- 'Opaleye.Lateral.laterally' 'distinct' :: 'Data.Profunctor.Product.Default' 'Distinctspec' fields fields => 'Opaleye.Select.SelectArr' i fields -> 'Opaleye.Select.SelectArr' i fields
 -- @
 distinct :: D.Default Distinctspec fields fields =>
+            D.Default Unpackspec fields fields =>
             Select fields -> Select fields
-distinct = distinctExplicit D.def
+distinct = distinctExplicit D.def D.def
diff --git a/src/Opaleye/Experimental/Enum.hs b/src/Opaleye/Experimental/Enum.hs
--- a/src/Opaleye/Experimental/Enum.hs
+++ b/src/Opaleye/Experimental/Enum.hs
@@ -1,4 +1,3 @@
-{-# LANGUAGE FlexibleContexts #-}
 {-# LANGUAGE ConstraintKinds #-}
 
 module Opaleye.Experimental.Enum
diff --git a/src/Opaleye/FunctionalJoin.hs b/src/Opaleye/FunctionalJoin.hs
--- a/src/Opaleye/FunctionalJoin.hs
+++ b/src/Opaleye/FunctionalJoin.hs
@@ -2,9 +2,6 @@
 -- See "Opaleye.Join" for details on the best way to do other joins in
 -- Opaleye.
 
-{-# LANGUAGE FlexibleContexts      #-}
-{-# LANGUAGE MultiParamTypeClasses #-}
-
 module Opaleye.FunctionalJoin (
   -- * Full outer join
   fullJoinF,
diff --git a/src/Opaleye/Internal/Aggregate.hs b/src/Opaleye/Internal/Aggregate.hs
--- a/src/Opaleye/Internal/Aggregate.hs
+++ b/src/Opaleye/Internal/Aggregate.hs
@@ -129,43 +129,31 @@
 --     https://github.com/tomjaguarpaw/haskell-opaleye/pull/460#issuecomment-626716160
 --
 -- Instead of detecting when we are aggregating over a field from a
--- previous query we just create new names for all field before we
+-- previous query we just create new names for all fields before we
 -- aggregate.  On the other hand, referring to a field from a previous
 -- query in an ORDER BY expression is totally fine!
 aggregateU :: Aggregator a b
-           -> (a, PQ.PrimQuery, T.Tag) -> (b, PQ.PrimQuery)
-aggregateU agg (c0, primQ, t0) = (c1, primQ')
-  where (c1, projPEs_inners) =
+           -> (a, T.Tag) -> (b, PQ.PrimQuery -> PQ.PrimQuery)
+aggregateU agg (c0, t0) = (c1, primQ')
+  where projPEs_inners :: PQ.Bindings HPQ.Aggregate
+        (c1, projPEs_inners) =
           PM.run (runAggregator agg (extractAggregateFields t0) c0)
 
-        projPEs = map fst projPEs_inners
-        inners  = concatMap snd projPEs_inners
+        projPEs = projPEs_inners
 
-        primQ' = PQ.Aggregate projPEs (PQ.Rebind True inners primQ)
+        primQ' = PQ.Aggregate projPEs
 
 extractAggregateFields
-  :: Traversable t
-  => T.Tag
-  -> (t HPQ.PrimExpr)
-  -> PM.PM [((HPQ.Symbol,
-              t HPQ.Symbol),
-              PQ.Bindings HPQ.PrimExpr)]
-           HPQ.PrimExpr
+  :: T.Tag
+  -> HPQ.Aggregate
+  -> PM.PM (PQ.Bindings HPQ.Aggregate) HPQ.PrimExpr
 extractAggregateFields tag agg = do
   i <- PM.new
-
-  let souter = HPQ.Symbol ("result" ++ i) tag
-
-  bindings <- for agg $ \pe -> do
-    j <- PM.new
-    let sinner = HPQ.Symbol ("inner" ++ j) tag
-    pure (sinner, pe)
-
-  let agg' = fmap fst bindings
+  let sinner = HPQ.Symbol ("result" ++ i) tag
 
-  PM.write ((souter, agg'), toList bindings)
+  PM.write (sinner, agg)
 
-  pure (HPQ.AttrExpr souter)
+  pure (HPQ.AttrExpr sinner)
 
 unsafeMax :: Aggregator (C.Field a) (C.Field a)
 unsafeMax = makeAggr HPQ.AggrMax
diff --git a/src/Opaleye/Internal/Binary.hs b/src/Opaleye/Internal/Binary.hs
--- a/src/Opaleye/Internal/Binary.hs
+++ b/src/Opaleye/Internal/Binary.hs
@@ -1,7 +1,5 @@
 {-# OPTIONS_HADDOCK not-home #-}
 
-{-# LANGUAGE MultiParamTypeClasses, FlexibleContexts #-}
-
 module Opaleye.Internal.Binary where
 
 import           Opaleye.Internal.Column (Field_(Column), unColumn)
diff --git a/src/Opaleye/Internal/Column.hs b/src/Opaleye/Internal/Column.hs
--- a/src/Opaleye/Internal/Column.hs
+++ b/src/Opaleye/Internal/Column.hs
@@ -3,7 +3,6 @@
 {-# LANGUAGE ConstraintKinds #-}
 {-# LANGUAGE DataKinds #-}
 {-# LANGUAGE TypeFamilies #-}
-{-# LANGUAGE FlexibleInstances #-}
 
 module Opaleye.Internal.Column where
 
diff --git a/src/Opaleye/Internal/Constant.hs b/src/Opaleye/Internal/Constant.hs
--- a/src/Opaleye/Internal/Constant.hs
+++ b/src/Opaleye/Internal/Constant.hs
@@ -1,6 +1,5 @@
 {-# OPTIONS_HADDOCK not-home #-}
 
-{-# LANGUAGE FlexibleContexts, FlexibleInstances, MultiParamTypeClasses #-}
 {-# LANGUAGE DataKinds #-}
 
 module Opaleye.Internal.Constant where
diff --git a/src/Opaleye/Internal/Distinct.hs b/src/Opaleye/Internal/Distinct.hs
--- a/src/Opaleye/Internal/Distinct.hs
+++ b/src/Opaleye/Internal/Distinct.hs
@@ -1,26 +1,25 @@
 {-# OPTIONS_HADDOCK not-home #-}
 
-{-# LANGUAGE FlexibleContexts #-}
-{-# LANGUAGE MultiParamTypeClasses #-}
-
 module Opaleye.Internal.Distinct where
 
 import qualified Opaleye.Internal.MaybeFields as M
 import           Opaleye.Select (Select)
 import           Opaleye.Field (Field_)
-import           Opaleye.Aggregate (Aggregator, groupBy, aggregate)
+import           Opaleye.Aggregate (Aggregator, groupBy, aggregateExplicit)
 
 import qualified Data.Profunctor as P
 import qualified Data.Profunctor.Product as PP
 import           Data.Profunctor.Product.Default (Default, def)
+import           Opaleye.Internal.Unpackspec (Unpackspec)
 
 -- We implement distinct simply by grouping by all columns.  We could
 -- instead implement it as SQL's DISTINCT but implementing it in terms
 -- of something else that we already have is easier at this point.
 
-distinctExplicit :: Distinctspec fields fields'
+distinctExplicit :: Unpackspec fields fields
+                 -> Distinctspec fields fields'
                  -> Select fields -> Select fields'
-distinctExplicit (Distinctspec agg) = aggregate agg
+distinctExplicit u (Distinctspec agg) = aggregateExplicit u agg
 
 newtype Distinctspec a b = Distinctspec (Aggregator a b)
 
diff --git a/src/Opaleye/Internal/Inferrable.hs b/src/Opaleye/Internal/Inferrable.hs
--- a/src/Opaleye/Internal/Inferrable.hs
+++ b/src/Opaleye/Internal/Inferrable.hs
@@ -1,8 +1,6 @@
 {-# LANGUAGE UndecidableInstances #-}
-{-# LANGUAGE FlexibleContexts #-}
 {-# LANGUAGE TypeFamilies #-}
-{-# LANGUAGE MultiParamTypeClasses #-}
-{-# LANGUAGE FlexibleInstances #-}
+{-# LANGUAGE TypeOperators #-}
 {-# LANGUAGE DataKinds #-}
 
 module Opaleye.Internal.Inferrable where
diff --git a/src/Opaleye/Internal/Join.hs b/src/Opaleye/Internal/Join.hs
--- a/src/Opaleye/Internal/Join.hs
+++ b/src/Opaleye/Internal/Join.hs
@@ -1,4 +1,3 @@
-{-# LANGUAGE FlexibleContexts, FlexibleInstances, MultiParamTypeClasses #-}
 {-# LANGUAGE TypeFamilies #-}
 {-# LANGUAGE Arrows #-}
 
diff --git a/src/Opaleye/Internal/Lateral.hs b/src/Opaleye/Internal/Lateral.hs
--- a/src/Opaleye/Internal/Lateral.hs
+++ b/src/Opaleye/Internal/Lateral.hs
@@ -1,5 +1,3 @@
-{-# LANGUAGE FlexibleContexts #-}
-
 module Opaleye.Internal.Lateral
   ( lateral
   , viaLateral
diff --git a/src/Opaleye/Internal/Manipulation.hs b/src/Opaleye/Internal/Manipulation.hs
--- a/src/Opaleye/Internal/Manipulation.hs
+++ b/src/Opaleye/Internal/Manipulation.hs
@@ -1,7 +1,4 @@
-{-# LANGUAGE FlexibleContexts      #-}
-{-# LANGUAGE FlexibleInstances     #-}
 {-# LANGUAGE GADTs                 #-}
-{-# LANGUAGE MultiParamTypeClasses #-}
 
 module Opaleye.Internal.Manipulation where
 
diff --git a/src/Opaleye/Internal/MaybeFields.hs b/src/Opaleye/Internal/MaybeFields.hs
--- a/src/Opaleye/Internal/MaybeFields.hs
+++ b/src/Opaleye/Internal/MaybeFields.hs
@@ -1,12 +1,10 @@
 {-# OPTIONS_HADDOCK not-home #-}
 
 {-# LANGUAGE LambdaCase #-}
-{-# LANGUAGE MultiParamTypeClasses #-}
 {-# LANGUAGE Arrows #-}
-{-# LANGUAGE FlexibleContexts #-}
-{-# LANGUAGE FlexibleInstances #-}
 {-# LANGUAGE DeriveFunctor #-}
 {-# LANGUAGE TypeFamilies #-}
+{-# LANGUAGE TypeOperators #-}
 {-# LANGUAGE UndecidableInstances #-}
 
 module Opaleye.Internal.MaybeFields where
diff --git a/src/Opaleye/Internal/Operators.hs b/src/Opaleye/Internal/Operators.hs
--- a/src/Opaleye/Internal/Operators.hs
+++ b/src/Opaleye/Internal/Operators.hs
@@ -1,7 +1,6 @@
 {-# OPTIONS_HADDOCK not-home #-}
 
 {-# LANGUAGE ExistentialQuantification #-}
-{-# LANGUAGE FlexibleContexts, FlexibleInstances, MultiParamTypeClasses #-}
 {-# LANGUAGE ScopedTypeVariables #-}
 
 module Opaleye.Internal.Operators where
diff --git a/src/Opaleye/Internal/PrimQuery.hs b/src/Opaleye/Internal/PrimQuery.hs
--- a/src/Opaleye/Internal/PrimQuery.hs
+++ b/src/Opaleye/Internal/PrimQuery.hs
@@ -133,7 +133,7 @@
                   | Product   (NEL.NonEmpty (Lateral, PrimQuery' a)) [HPQ.PrimExpr]
                   -- | The subqueries to take the product of and the
                   --   restrictions to apply
-                  | Aggregate (Bindings (HPQ.Aggregate' HPQ.Symbol))
+                  | Aggregate (Bindings HPQ.Aggregate)
                               (PrimQuery' a)
                   | Window (Bindings (HPQ.WndwOp, HPQ.Partition)) (PrimQuery' a)
                   -- | Represents both @DISTINCT ON@ and @ORDER BY@
@@ -178,7 +178,7 @@
   , empty             :: a -> p'
   , baseTable         :: TableIdentifier -> Bindings HPQ.PrimExpr -> p'
   , product           :: NEL.NonEmpty (Lateral, p) -> [HPQ.PrimExpr] -> p'
-  , aggregate         :: Bindings (HPQ.Aggregate' HPQ.Symbol)
+  , aggregate         :: Bindings HPQ.Aggregate
                       -> p
                       -> p'
   , window            :: Bindings (HPQ.WndwOp, HPQ.Partition) -> p -> p'
diff --git a/src/Opaleye/Internal/Rebind.hs b/src/Opaleye/Internal/Rebind.hs
--- a/src/Opaleye/Internal/Rebind.hs
+++ b/src/Opaleye/Internal/Rebind.hs
@@ -1,5 +1,3 @@
-{-# LANGUAGE FlexibleContexts #-}
-
 module Opaleye.Internal.Rebind where
 
 import Data.Profunctor.Product.Default (Default, def)
diff --git a/src/Opaleye/Internal/RunQuery.hs b/src/Opaleye/Internal/RunQuery.hs
--- a/src/Opaleye/Internal/RunQuery.hs
+++ b/src/Opaleye/Internal/RunQuery.hs
@@ -1,6 +1,5 @@
 {-# OPTIONS_HADDOCK not-home #-}
 
-{-# LANGUAGE FlexibleContexts, FlexibleInstances, MultiParamTypeClasses #-}
 {-# LANGUAGE ConstraintKinds #-}
 {-# LANGUAGE DataKinds #-}
 
diff --git a/src/Opaleye/Internal/RunQueryExternal.hs b/src/Opaleye/Internal/RunQueryExternal.hs
--- a/src/Opaleye/Internal/RunQueryExternal.hs
+++ b/src/Opaleye/Internal/RunQueryExternal.hs
@@ -1,7 +1,5 @@
 {-# OPTIONS_HADDOCK not-home #-}
 
-{-# LANGUAGE FlexibleContexts #-}
-
 module Opaleye.Internal.RunQueryExternal
                  (module Opaleye.Internal.RunQueryExternal,
                          -- * Datatypes
diff --git a/src/Opaleye/Internal/Sql.hs b/src/Opaleye/Internal/Sql.hs
--- a/src/Opaleye/Internal/Sql.hs
+++ b/src/Opaleye/Internal/Sql.hs
@@ -160,7 +160,7 @@
           PQ.Lateral    -> Lateral
           PQ.NonLateral -> NonLateral
 
-aggregate :: PQ.Bindings (HPQ.Aggregate' HPQ.Symbol)
+aggregate :: PQ.Bindings HPQ.Aggregate
           -> Select
           -> Select
 aggregate aggrs' s =
@@ -191,7 +191,7 @@
         handleEmpty = ensureColumnsGen SP.deliteral
 
         aggrs :: [(Symbol, HPQ.Aggregate)]
-        aggrs = (map . Arr.second . fmap) HPQ.AttrExpr aggrs'
+        aggrs = aggrs'
 
         groupBy' :: [(symbol, HPQ.Aggregate)]
                  -> NEL.NonEmpty HSql.SqlExpr
diff --git a/src/Opaleye/Internal/Table.hs b/src/Opaleye/Internal/Table.hs
--- a/src/Opaleye/Internal/Table.hs
+++ b/src/Opaleye/Internal/Table.hs
@@ -1,5 +1,3 @@
-{-# LANGUAGE FlexibleInstances #-}
-{-# LANGUAGE FlexibleContexts #-}
 {-# LANGUAGE FunctionalDependencies #-}
 {-# LANGUAGE Rank2Types #-}
 
diff --git a/src/Opaleye/Internal/Unpackspec.hs b/src/Opaleye/Internal/Unpackspec.hs
--- a/src/Opaleye/Internal/Unpackspec.hs
+++ b/src/Opaleye/Internal/Unpackspec.hs
@@ -1,7 +1,5 @@
 {-# OPTIONS_HADDOCK not-home #-}
 
-{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances #-}
-
 module Opaleye.Internal.Unpackspec where
 
 import qualified Opaleye.Internal.PackMap as PM
diff --git a/src/Opaleye/Internal/Values.hs b/src/Opaleye/Internal/Values.hs
--- a/src/Opaleye/Internal/Values.hs
+++ b/src/Opaleye/Internal/Values.hs
@@ -1,5 +1,4 @@
 {-# LANGUAGE Arrows #-}
-{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses #-}
 {-# LANGUAGE ScopedTypeVariables #-}
 {-# LANGUAGE LambdaCase #-}
 
diff --git a/src/Opaleye/Join.hs b/src/Opaleye/Join.hs
--- a/src/Opaleye/Join.hs
+++ b/src/Opaleye/Join.hs
@@ -1,8 +1,5 @@
 -- | Left, right, and full outer joins.
 
-{-# LANGUAGE FlexibleContexts      #-}
-{-# LANGUAGE FlexibleInstances     #-}
-{-# LANGUAGE MultiParamTypeClasses #-}
 {-# LANGUAGE TypeFamilies          #-}
 
 module Opaleye.Join where
diff --git a/src/Opaleye/Manipulation.hs b/src/Opaleye/Manipulation.hs
--- a/src/Opaleye/Manipulation.hs
+++ b/src/Opaleye/Manipulation.hs
@@ -1,6 +1,4 @@
 {-# LANGUAGE ExistentialQuantification #-}
-{-# LANGUAGE FlexibleContexts          #-}
-{-# LANGUAGE FlexibleInstances         #-}
 {-# LANGUAGE GADTs                     #-}
 {-# LANGUAGE MultiParamTypeClasses     #-}
 
diff --git a/src/Opaleye/MaybeFields.hs b/src/Opaleye/MaybeFields.hs
--- a/src/Opaleye/MaybeFields.hs
+++ b/src/Opaleye/MaybeFields.hs
@@ -1,5 +1,3 @@
-{-# LANGUAGE FlexibleContexts #-}
-
 -- | 'MaybeFields' is Opaleye's analogue to 'Data.Maybe.Maybe'.  You
 -- probably won't want to create values of type 'MaybeFields'
 -- directly; instead they will appear as the result of
diff --git a/src/Opaleye/Operators.hs b/src/Opaleye/Operators.hs
--- a/src/Opaleye/Operators.hs
+++ b/src/Opaleye/Operators.hs
@@ -1,8 +1,6 @@
 {-# LANGUAGE Arrows #-}
-{-# LANGUAGE FlexibleContexts #-}
 {-# LANGUAGE ConstraintKinds #-}
 {-# LANGUAGE TypeSynonymInstances #-}
-{-# LANGUAGE MultiParamTypeClasses #-}
 {-# LANGUAGE FunctionalDependencies #-}
 {-# LANGUAGE DataKinds #-}
 
diff --git a/src/Opaleye/Order.hs b/src/Opaleye/Order.hs
--- a/src/Opaleye/Order.hs
+++ b/src/Opaleye/Order.hs
@@ -1,5 +1,3 @@
-{-# LANGUAGE FlexibleContexts #-}
-
 -- | @ORDER BY@, @LIMIT@, @OFFSET@ and @DISTINCT ON@
 
 module Opaleye.Order ( -- * Order by
diff --git a/src/Opaleye/RunSelect.hs b/src/Opaleye/RunSelect.hs
--- a/src/Opaleye/RunSelect.hs
+++ b/src/Opaleye/RunSelect.hs
@@ -1,4 +1,3 @@
-{-# LANGUAGE FlexibleContexts #-}
 {-# LANGUAGE PolyKinds #-}
 {-# LANGUAGE TypeFamilies #-}
 
diff --git a/src/Opaleye/Sql.hs b/src/Opaleye/Sql.hs
--- a/src/Opaleye/Sql.hs
+++ b/src/Opaleye/Sql.hs
@@ -1,4 +1,4 @@
-{-# LANGUAGE FlexibleContexts, ScopedTypeVariables #-}
+{-# LANGUAGE ScopedTypeVariables #-}
 
 module Opaleye.Sql (
   -- * Showing SQL
diff --git a/src/Opaleye/Table.hs b/src/Opaleye/Table.hs
--- a/src/Opaleye/Table.hs
+++ b/src/Opaleye/Table.hs
@@ -1,7 +1,3 @@
-{-# LANGUAGE FlexibleContexts #-}
-{-# LANGUAGE FlexibleInstances #-}
-
-
 {- |
 
  Table fields can be required or optional and, independently, nullable or
diff --git a/src/Opaleye/ToFields.hs b/src/Opaleye/ToFields.hs
--- a/src/Opaleye/ToFields.hs
+++ b/src/Opaleye/ToFields.hs
@@ -1,5 +1,3 @@
-{-# LANGUAGE FlexibleContexts #-}
-
 module Opaleye.ToFields (-- * Creating 'Field's from Haskell values
                          toFields,
                          toFieldsI,
diff --git a/src/Opaleye/Values.hs b/src/Opaleye/Values.hs
--- a/src/Opaleye/Values.hs
+++ b/src/Opaleye/Values.hs
@@ -1,5 +1,3 @@
-{-# LANGUAGE FlexibleContexts #-}
-
 module Opaleye.Values(
   values,
   -- * Explicit versions
diff --git a/src/Opaleye/With.hs b/src/Opaleye/With.hs
--- a/src/Opaleye/With.hs
+++ b/src/Opaleye/With.hs
@@ -1,5 +1,3 @@
-{-# LANGUAGE FlexibleContexts #-}
-
 module Opaleye.With
   ( with,
     withMaterialized,
