VP-297: Code improvement and fixed most unit tests.