Ugly and dirty, yet working version