Implemented select for main loop.