Fixes various bugs