refactor the mess that is the ajax code