Added some more sanity checks for input parameters, and correct type coercions (string->number).