make onbefore calls more robust