code improvement for better performance + bugfixes