bugfixes and better script logic