Cleaned up code, added the option to specify pattern either as a regex or a string