Added a couple of minor error handling checks etc.