small code tidying up and performance improvements