Refactor monomial implementation and tests