Fix exception caused by improper element.remove by moving it to setTimeout