rewrite to use Matrix package