Refactor

Fixes for these issues:

https://issues.apache.org/jira/browse/CB-6279