fixing unusual bug on safari, maybe related to variable/function hoisting