Optimization, fixing broken code