more refactoring to simplify code