Refactored some balance query code into calculateAccountBalances