Combined Josh's code