messy inference code