moved dirname test before api call