modified attach event function.