refactored color change function