replaced IF segmentation with INTERVAL, some cleanups