some optimization and fixes