refactoring and solution for jajascript flight optim