Refactor geometry functions