Fixed more bugs and added more debugging functions

Many (silly) bugs were fixed.