Improved error checking (write stacktrace to log file)