working. code is ugly tho