rewrite render loop and fix various issues