Fixed defer calls and removed obsolete function