Improve edge case handing for dynamic binary operations

Also moving to T4 templates to reduce duplicated code as the functions get more complex