generify code a bit for adding listeners