Some refactor, added comments.