diff --git a/pub/lib/plot.cpp b/pub/lib/plot.cpp
index a7161ccac00abd63a23305f44e89a1966366dd77..cbf043273b42f783b3c165655375dd4dcc93f161 100644
--- a/pub/lib/plot.cpp
+++ b/pub/lib/plot.cpp
@@ -294,6 +294,8 @@ int plot_curve_refine(CPlot* plot, const COlc* fc, int k, int j, int cstyle)
         }
         return ret;
     };
+    auto pt_order = [](const CPt a, const CPt b) -> bool { return a.xd<=b.xd; };
+
     vector<CPt> pp;
 
     // start with equidistant grid:
@@ -305,6 +307,7 @@ int plot_curve_refine(CPlot* plot, const COlc* fc, int k, int j, int cstyle)
     // refinement loop:
     for (int iref=0; iref<20; ++iref) {
         vector<CPt> pnew = x2p(xnew);
+        pp = triv::merge_sorted(pp, pnew, pt_order);
     }
 
     // divide into segments, and plot
diff --git a/pub/trivia/vector_ops.hpp b/pub/trivia/vector_ops.hpp
index da19ecfc8f3fa0b064735d466077b9db982a8a85..894f6b4bd62b210ed032134f6d67ccfb7211dd8f 100644
--- a/pub/trivia/vector_ops.hpp
+++ b/pub/trivia/vector_ops.hpp
@@ -25,9 +25,8 @@ namespace triv
     std::string indices_to_s(const std::vector<int>& v);
 
     template <class T> bool contains(const std::vector<T>& v, const T e);
-    template <class T> std::vector<T> merge_sorted(
-        const std::vector<T>& a, const std::vector<T>& b,
-        std::function<bool(const T, const T)> a_before_b);
+    template <class T, class Pred> std::vector<T> merge_sorted(
+        const std::vector<T>& a, const std::vector<T>& b, Pred a_before_b);
 
 //**************************************************************************************************
 //  Template implementation
@@ -38,16 +37,18 @@ namespace triv
         return std::find(v.begin(), v.end(), e) != v.end();
     }
 
-    template <class T>
-    std::vector<T> merge_sorted(
-        const std::vector<T>& a, const std::vector<T>& b,
-        std::function<bool(const T, const T)> a_before_b)
+    template <class T, class Pred>
+    std::vector<T> merge_sorted(const std::vector<T>& a, const std::vector<T>& b, Pred a_before_b)
     {
         std::vector<T> ret;
         auto ia=a.begin();
         auto ib=b.begin();
-        for (;;ia!=a.end() || ib!=ib.end())
+        for (;ia!=a.end() && ib!=b.end();)
             ret.push_back( a_before_b(*ia,*ib) ? *(ia++) : *(ib++) );
+        for (;ia!=a.end();)
+            ret.push_back( *(ia++) );
+        for (;ib!=b.end();)
+            ret.push_back( *(ib++) );
         return ret;
     }
 }