Combined both algorithms