Fixed off-by-one error and handled malformed arguments