Code cleanup, changed scoping