Use patterns instead of string manipulation

make code much more easier to follow