Improve error handling
Refactor logging