improved code factorization, removed a few useless stuffs