cleaned and improved code