fixed serious bug in alloc code

thanks to Crigges for discovering it