better error handling, stats reporting and code structure