More variable substution, tests and tidyups