Fix of incorrect variable reference and refactor code to use more concise varnames