Refactorization: Avoid iterations over whole token list, limited several checks to function scopes.