implementing minmax... this is so close to being right