Added some error checkings on input parameters.