Error correction and code cleaning