Correctly handle closure of sample file