Added some checks to the previously changed code