refactoring logic out of request handler