CLOUDIFY-2222 some refactoring and fixes