We need to clean up this routine a little bit, but it should work nonetheless