Refactor error handling

:godmode: