improve ajax logic