refact logic