Rearranged code to try to deal with race-conditions on startup.