Modified selection logic. Now it is possible to select nodes that are not foremost