Use with statement in scan

This is a proposal to fix #46.