Refactored and simplified the edge handling