Fixed safe-convert function