some code refactoring for clarity