Optimalizations and fixes