Cleaned up the calculation, removed global variable usage.