Added new functions to Magma, refactored.