code that calculates msf in a timewindow, split in functions