Fixes several bugs