Why are variables undefined suddenly?