optimize agal code