Added error messages and a few tidy ups