Added merge feature and some optimization.