move most of domain code from pyx to py