Moved code so that it runs when device is ready.