corrected a problem with pos array + free_tree
added free_patterns