Added debug and error messages etc.