Modularize rectangle detection. This will soon be used for other things.