Fix switch statement

+ add error messages
+ open file on demand instead of every time
+ close file