Re-added middleware and also implemented try/catch instead of filesystem check.