Combined code

Combined Andrew's code and Daylan's code.