moved some of the code around