Fix error "Can't set headers after they are sent."