Limit replacements, some logic + coding-style errors fixed