factorize logic