code cleanup, use stream wrapper and csv instead of manual stuff