moved a lot of things around to make code more readable