Refactoring Math library