restructuring code for ajax requests