Fixes #267

Total day count can be wrong (because of not sorting). This is a duplicate of @dolfje's pull-request #268 except it merges into the development branch.