add better error handling and polished up some stats functionality