extracted function processEvents