implementation of modified algorithm