Adding more logging and using error codes