refactor out neighbors rearrangement JavaScript code into separate module