Substring function does not work as expected. Researching alternatives...