Import StringIO and exception handling fixes