Refector and simplify code