Checking for empty string and a little refactoring