code cleanup and minor improvements