cleanup and minor optimiziations