Tidied up the functions and added more error messages for the log
file