Add polling implementation and fix some bugs.