edited code for auto motion.