Made variables local (to stop them leaking outside the function)