Try using context handlers.