Ugly code, works