Wrote version without sampling and debugged