Some bugfixes and sync with master