completed inference function and verified with test cases, woohoo