Minor optimizations, bugfixes, added documentation