works but code needs lots of cleanup