cleanup and refactoring - stage4