Implemented delayed close using Iasoon's code