Optimization and library fixes