Fixed bin-counting, refactoring.