adding gevent support and required refactoring