Problem looping through mesh objects and joining

I am working on a script to do the following on a mesh imported from an .x3d file:

Delete an unwanted viewpoint

Loop through all of the mesh objects in the scene and: turn on auto smooth, add an edge split modifier, add a subsurf modifier, smooth

Loop though all of the mesh objects in the scene and: select all of the other objects with same rgb diffuse colour, link them all by material, join them into a single mesh.

Adding the modifiers and smoothing works fine, but it crashes when I try to do the joining part. The part of the script to select all of the objects with same rgb diffuse colour works fine in isolation.

I only started python/blender scripting a few days ago and this is my first proper script, so apologies if it is a bit rough and unrefined.

Thanks

import bpy

process mesh for every object

def smooth():
obj= bpy.data.objects
for ob in obj:
if ob.type == ‘MESH’:
bpy.context.scene.objects.active = ob

		# turn autosmooth on
		bpy.context.object.data.use_auto_smooth = True


		# edgesplit modifier
		bpy.ops.object.modifier_add(type = 'EDGE_SPLIT')


		# subsurf modifier
		bpy.ops.object.modifier_add(type = 'SUBSURF')


		# smooth
		for pol in ob.data.polygons:
			pol.use_smooth = True

select objects by diffuse colour

def colourSelect():
col = bpy.context.object.data.materials[0].diffuse_color
mess = [o for o in bpy.context.scene.objects if o.type == ‘MESH’]

for obj in mess:
	list = []
	for mat in obj.data.materials:
		try: list.append(mat.diffuse_color)
		except: pass
	obj.select = col in list

link by material and join mesh

def join():
# link by material
bpy.ops.object.make_links_data(type = ‘MATERIAL’)

#link by material
bpy.ops.object.join()

deselect all objects

bpy.ops.object.select_all(action = “DESELECT”)

delete Viewpoint

bpy.ops.object.select_pattern(pattern=“Viewpoint”)
bpy.ops.object.delete(use_global=False)

smooth()

obj= bpy.data.objects
for ob in obj:
if ob.type == ‘MESH’:
bpy.context.scene.objects.active = ob
colourSelect()
join()

Any help would be much appreciated.