Removed the NPE and implemented proper Error handling