Refactored middleware and app mounting.