Fixed a few crucial bugs. There is still one more I'll fix later.