Cleanup rectangle fiter code and add unit tests