more refactoring in apply.c