ugly code, nice result