Added try catch blocks and null checks