Moved some code around