Refactor error calculation stuff