efactored the code