improved events code