Refactor middleware handling. Pass response now to application.