Moved widget attaching and event wiring inside loop