Complete reimplementation

Note: needs a version of scala that has issue SI-7343 solved
(https://issues.scala-lang.org/browse/SI-7343)