added the ability to pass a function to count weights