Memory leak fixes, added difference funciton and example for motion detection