Fixed bug in motion detection