Move run code to function