Here invalid input data will break everything. Please add some kind of validation. I think a couple of asserts will be enough.