did some small optimizations + commenting, added test