Move event registration to a separate function