Error handeling refactor