sooo ugly code that somehow works