Moved lots of code around