Fixed a few bugs. And cleaned the code a tiny bit!