Move safe_basename into utils and add a test