first refactor of JS ajax call -- moved into functions