Various bug fixing