Sync C++ model code with Java implementation