Full rewrite (optimizations; added nearly infinite time support)