Moved code into function.