refactored code and added conditional formatting