Removed uses of defer. The order of execution is now solid and not exceptable to race conditions.