moved helper functions outside fetch function