Defer is not working the way I expect it to