Enhanced the box adding algorithm