made algorithm more sound