Referencing local variable before assignment results in unexpected behavior