perf fixes in select and code cleanup