Refactored IO methods to take blocks to automatically close files