Reworking math code