Cleanup code, add a 'refreshTokenIfNeeded' function

Hi #5