UK: code refactor: ensure file is closed properly