Code cleanup

Cache variables, cleanup use of undefined, use common logger, etc.