refectored the code