Working version with correct testing (though poor results)