Cleaned, formatted and fixed

Cleaned the code, formatting included.
Fixed a few errors (some that would have spammed the console)
Made the code more efficient.