fixed a bug on threading.
added comments.