added a somewhat intelligent learning function that actually works