Refectored the code