added xhr connection and fixed control flow in system