Pattern generator works, but realy slow. Need optimisation