Fix some loop logic and move cache files