Cleaning up matrix code