new diagonal algorithm