an elegant alrogithm