Some improvements and fixes

There might be more to come if I get a chance to take a deeper look.