Added sample counting, not perfect.