Optimize and simplify fractions code.