optimized sameheight function. still not perfect