[MOD] Numerous rewritings, optimizations, bug fixes