improved imperative code