Data.List.nub is O(n^2); replaced it with an equivalent O(n*log(n)) implementation.