Optimized and refactored render code