Various bug fixes and improvements

It actually works now!