Finally fixed bug in which sample was occasionally causing an exception.