Fixing errors that can be missed easily and a resource leak