implemented matt's code