Lots of fixes for broken code and loops.