Move sampling logic into javascript
Merge branch 'temp'