Refactor to avoid some redundant substring calls