Code cleanup, file leak fix