Fixed script not terminating correctly