added scope checking of fields
merge and moved helper functions down in the AG file