Working version of motion detection