Refactored render_video function