refactored ML code