Fixed mysterious for loop, among other errors