Some fixes and optimization