added a lot of comments for further optimization