Rework the entire check for available blocks and chunk allocation code.
Why does the check blocks available allocate the chunks? Wouldn't it be better to allocate them as the reference IDs are generated? Have the check blocks available function just verify that there is enough storage in a form that will work.
Maybe it should try to randomize even in minimal if all of the existing chunks are too dense?