refracted the code a bit