Remove unused variables, fix minor bugs