refector code a bit