fixed maxz and maxchi to work in extreme cases