Improve code and add documentation to Math functions