Infinite loops occurring after alert. needs debugging