Added some safety around reading files, better warnings and error messages and tidied things up a bit