Reorganised program and finished consistency check functions