Attach document listeners only when needed