several fixes and code improvements