refactored boundary extraction slightly