Fixing several bugs