messy... but works