Cost based output optimisation in place, code not massively efficient but compression working as it should