Refactor and add mathcheck