Add a generic addListener function to reduce repetition.