Implement middleware layer