improved code for different cases