Refactor render code