refactored for easier tweaking