null checks added.
code refactored.