error fixes and optimization