some code refactored