combined Terrance's code with Daniel's