refactored the last middleware