fix markup-like parsing, minor refactoring, optimizations