general fixes and optimization