some optimization and fixing