More generalized code with better comments.