Fixed resource leak by closing CodeWriter when finishing translation.

Use flag ordering properly.