note that method_source uses a similar algorithm