cleaned up code, implemented motion detection