improving AM extraction script