fixing and reshaping some code, the optimizer is working now