completely rewritten pattern generation functions