factorize and clean up (bug 983749)