Cleanup and reorganization.
A few optimizations.
Variable first sampling interval.