Added select function, as well as refactored some things to reuse code.