minor bug fixes and optimization