Bug fixes

These are just the fixes already outlined in the comments of the issues, fixed while getting familiar with the code.