Bug fixes, thanks @waader