improve critical region

I think this is a much more performant approach to implement critical regions.