migrated some of the pattern recognition code