moved on_end to before the client is closed and also sends app down