code simplified and parametrized