Optimization and bugfixes