The final code, hope i did not mess something up
stupid merge, too lazy to fix