restructuring, inspired by sklearn