Fixing various bugs