null checks added and throw new err changed