JA-4708 Fixed bugs and added tests.