improved height calculation algorithm