Updated to use less slow exception throwing, and string joining for python 2 to 3 workaround.