there's some lazy code in here, but it works