refactoring js. Added merge function. Don't want to use too much of jQuery