Refactoring

Optimization (math) algorithms moved to separate functions