Horrible programming but it works.