some optimization, refactoring