Loads of small bug fixes and code improvements