Fixed a bug for context manager when the results might have been
overwritten.
Move initialization code outside the function wrapper when used as a
decorator.