bugfix + optimization

Optimized and fixed bugs (tested)