Refactor the code and add null validation