Add guards for missing references and fix tests