fix race conditions in lazy-load

Fix several race conditions and make code somewhat more robust
against future changes.