fixed while condition based on undefined variable