fixed loops and thumbnail control and cleaned up main loop