adjusted algorithm to get proper date