Fixes several issues

@pebeah please review so far