substrings and cast errors fixed