Refactored code: middleware extracted to separate folder