A less kludgy implementation