refactored implementation of gmatrix.jl