working, but code not pretty