Please refactor and refine the code with optimized and enhanced error handling and implement robust upscaling, expanding and building upon this.