ported to plain C++