fixes and optimization