better algorithm
merging
commented out some of mainak's bullshit, added comment