Refactored sandbox initialisation code for efficiency and reentrancy