Some cleanup, better error handling, validate formats