cleaned up collapse function