MAINT: use cython classes with polymorphic functions instead of copypasting code