streamline and refactor error handling