cleaning up matrix code