integrated commit fe4728488b673837354c3bbda97c9cc39849e924 from ktaranov/ecmergeru together with a few fixes
integrated commit 400e532317 from Konstantin Taranov