Various fixes for proper behavior. Couldn't test previously since there weren't any open pull requests.