Whole program works correctly now.