Improved events code