updated algoritm and checking of input data