Refactored request listener function