improved time detection algorithm