added a purchase optimizer, fixed some bugs