Refactor sample to remove ifs statements with polymorphism