tidy up and fix 2 potential bugs