added self-balancing rebalance function - took hours but works nicely