Added some better assertions and handling of input