Improved listener code