refactor render loop