Motion detection code