modified algorithm and documentation