Optimised C++ and D version