Working extension. Algorithm to be improved.