minor refactoring and cleanup
fixed a latent bug