Added error checking code