add pkg-friendly file headers
fix variable scoping issue