Improve statistics

To enable per-community statistics + some cleanup.

DO NOT MERGE YET. Many changes needed to be done for gumby and tribler.