Moved functionalities to a decorator and a context manager