nicer implementation based off of PR#12