Added some refinements, also detecting problems with time gaps.