Fixed various bugs across the code.

Everything works as expected now.