Fixed the IIFE scope issue. The initial code was not taking into account DOM ready state.