Refactored according to comments.