Improved rectangle selection code