restructuring a bit, inspired by sklearn