Added error check and few comments