Minimize the critical region

Signed-off-by: Jonas Kalderstam <jonas@kalderstam.se>