Added some fixes and removed debug writefln()s