added a bunch of comments for clarity