substantial refactoring to get rid of loading race condition