refactored area detection code again: now using a helper function for better readability and maintainability