CFY-1049 refactoring + cleanup + bug fix