Based off of rsw_rect_reduced.m by Ben Storer