lots of comments and some optimizations