improved selection code, cleanups