My proposed patch for some of the issues I raised