added safety checks for references