Improved errohandling and logging