improved and greatly simplified coroutine implementation