Refactored rect generation code