cleanup matrix code