cleaned up main loop to get rid of stupid transposes