moved a lot of code around