Very fun algorithm