poor code, but calculates and writes ranges