Fixing broken templateing logic - it got confused in certain senarios
replacing document with passed dom argument