refactored code to make it reusable for events api