Refactoring code for issue #386, loop not needed