ugly code, but hey, it works!