CORE-4729: refactored the code to clean it up a little