Optimization and build fixes