Some math fixes suggested by @eltjpm in #298