Modularized the code a bit and should work 99%