Fixed a bug by changing the loop in max. It now splits from left to right.