Fixed a couple of buggs

It is now ready for testing.