Subtle bug with logic