finished code

99% sure it works just have to test it