move function definitions
make some functions protected