Fix file leakage and tidy up.