added error logging and debug levels