Replaced a lot of inline code with call to find_and_flag function.