some cleanup work + refactoring