Improvements on cpp version