Refactored null check and .trim() application to make code more readable