Moved optimization step to Optimize.js and minor improvements