r code filled in after prototype comments
tested on new, replace matrices