added function and minimize code