Simlifie code with contextmanager