refactoring some of the ajax code