completly works, but code is ugly