Imported Math code from tsoc