Used AAA-style in optimization function.
Added some comments to the code for documentation.