bug fixes thanks to Michael Saunders